﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;

namespace JWFW.Model.XMLSetting
{
    /// <summary>
    /// 操作保存为XML的类的接口
    /// </summary>
    public class XMLConfigOP
    {
        /// <summary>
        /// 创建一个XML元素
        /// </summary>
        /// <param name="parent">父节点</param>
        /// <param name="label">元素的标签名</param>
        /// <returns>创建的xml节点</returns>
        public static XmlNode CreateElement(XmlNode parent, string label)
        {
            XmlDocument doc = parent.NodeType == XmlNodeType.Document ? (XmlDocument)parent : parent.OwnerDocument;
            XmlElement ele = doc.CreateElement(label);
            parent.AppendChild(ele);
            return ele;
        }
        /// <summary>
        /// 给XML节点添加属性
        /// </summary>
        /// <param name="node">节点</param>
        /// <param name="attribute">属性名</param>
        /// <param name="val">属性值</param>
        public static void AddAttribute(XmlNode node, string attribute, object val)
        {
            XmlDocument doc = node.NodeType == XmlNodeType.Document ? (XmlDocument)node : node.OwnerDocument;
            XmlAttribute att = doc.CreateAttribute(attribute);
            if (val == null)
            {
                val = "";
            }
            att.Value = val.ToString();
            node.Attributes.Append(att);
        }
        /// <summary>
        /// 给XML节点添加一系列具有指定接口的子节点
        /// </summary>
        /// <param name="node">节点</param>
        /// <param name="children">子节点</param>
        /// <param name="label">子节点所在的描述集合的节点的标签名</param>
        public static void AddChildren(XmlNode node, IXML[] children, string label)
        {
            XmlDocument doc = node.NodeType == XmlNodeType.Document ? (XmlDocument)node : node.OwnerDocument;
            XmlElement ele = doc.CreateElement(label);
            node.AppendChild(ele);
            for (int i = 0; i < children.Length; i++)
            {
                children[i].Save(ele);
            }
        }

        /// <summary>
        /// 加载String值
        /// </summary>
        /// <param name="node">节点</param>
        /// <param name="attribute">属性名</param>
        /// <returns>String值</returns>
        public static string GetStringAttribute(XmlNode node, string attribute)
        {
            return node.Attributes[attribute].Value;
        }
        /// <summary>
        /// 加载Int值
        /// </summary>
        /// <param name="node">节点</param>
        /// <param name="attribute">属性名</param>
        /// <returns>Int值，默认为0</returns>
        public static int GetIntAttribute(XmlNode node, string attribute)
        {
            int res = 0;
            int.TryParse(GetStringAttribute(node, attribute), out res);
            return res;
        }
        /// <summary>
        /// 加载Bool值
        /// </summary>
        /// <param name="node">节点</param>
        /// <param name="attribute">属性名</param>
        /// <returns>Bool值，默认为false</returns>
        public static bool GetBoolAttribute(XmlNode node, string attribute)
        {
            bool res = false;
            bool.TryParse(GetStringAttribute(node, attribute), out res);
            return res;
        }
    }
}
